home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / newlooklib.lha / newlook / scalebar.c < prev    next >
C/C++ Source or Header  |  1993-11-07  |  3KB  |  100 lines

  1. /*
  2.  *  SCALEBAR.C
  3.  */
  4.  
  5. #include "newlook.h"
  6.  
  7. extern void CopyMem(APTR,APTR,ULONG);
  8. extern VOID DrawImage( struct RastPort *, struct Image *, WORD, WORD );
  9. extern VOID DrawBorder( struct RastPort *, struct Border *, WORD, WORD );
  10.  
  11. struct ScalebarInfo *CreateScalebar(x,y,w,h, nmin,nmax)
  12. SHORT x,y,w,h;
  13. LONG nmin, nmax;
  14. {
  15.   struct ScalebarInfo *si;
  16.   struct Image *i;
  17.   struct Border *b;
  18.  
  19.   ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);
  20.  
  21.   if(si= (struct ScalebarInfo *)SmartAllocate(SCALEBARINFOSIZE))
  22.   {
  23.     if(i= (struct Image *)SmartAllocate(3*IMAGESIZE))
  24.     {
  25.       if(w < SCALEBARHMIN) w= SCALEBARHMIN;
  26.       if(h < SCALEBARVMIN) h= SCALEBARVMIN;
  27.  
  28.       if(b= (struct Border *)CreateBorder(x,y, w,h, FALSE))
  29.       {
  30.         long r= nmax - nmin;  /* range */
  31.  
  32.         si->wmin        = (nmin<0) ? (nmin*(w-8)+r/2)/r : 0L;
  33.         si->wmax        = (LONG)w-8 + si->wmin;
  34.         si->outline     = b;
  35.         si->scalebar    = &i[0];
  36.         si->nmin        = nmin;
  37.         si->nmax        = nmax;
  38.  
  39.         i[0].TopEdge    = y+2;
  40.         i[0].Height     = h-4;
  41.         i[0].Depth      = 0;
  42.         i[0].ImageData  = (UWORD *)NULL;
  43.         i[0].PlanePick  = 0x00;
  44.  
  45.         CopyMem((APTR)&i[0],(APTR)&i[1],IMAGESIZE);
  46.         CopyMem((APTR)&i[0],(APTR)&i[2],IMAGESIZE);
  47.  
  48.         i[0].LeftEdge   = x+2;
  49.         i[0].PlaneOnOff = 0;
  50.         i[0].NextImage  = &i[1];
  51.  
  52.         i[1].PlaneOnOff = SCALEBARPEN;   /* trick! */
  53.         i[1].NextImage  = &i[2];
  54.  
  55.         i[2].PlaneOnOff = 0;
  56.         i[2].NextImage  = (struct Image *)NULL;
  57.  
  58.         MakePrivateHandlePublic(UserHandle);
  59.         return si;
  60.       }
  61.     }
  62.   }
  63.   if(UserHandle != PRIVATE_HANDLE)
  64.   { SmartFreeAll(PRIVATE_HANDLE);
  65.     (void)SetNewLookHandle(UserHandle);
  66.   }
  67.   return (struct ScalebarInfo *)NULL;
  68. }
  69.  
  70.  
  71. void SetScalebar(w,si,n)
  72. struct Window *w;
  73. struct ScalebarInfo *si;
  74. LONG n;
  75. {
  76.   struct Image *i;
  77.  
  78.   if(si && (i= si->scalebar))
  79.   {
  80.     LONG N= si->nmax - si->nmin;  /* numerical range */
  81.     LONG G= si->wmax - si->wmin;  /* graphical range */
  82.  
  83.     LONG W= (n * G + N/2) / N;    /* blue bar's width */
  84.  
  85.     if(W < si->wmin) W= si->wmin;
  86.     else if(W > si->wmax) W= si->wmax;
  87.  
  88.     /*printf("%ld <= (W=%ld) <= %ld\n",si->wmin,W,si->wmax);*/
  89.  
  90.     i[0].Width= 2 - si->wmin + ( (W<0) ? W:0 );
  91.     i[1].LeftEdge= i[0].LeftEdge + i[0].Width;
  92.     i[1].Width= ( (W<0) ? -W : W );
  93.     i[2].LeftEdge= i[1].LeftEdge + i[1].Width;
  94.     i[2].Width= G - i[0].Width - i[1].Width + 4;
  95.  
  96.     DrawImage(w->RPort, i, 0,0);
  97.     DrawBorder(w->RPort, si->outline, 0,0);
  98.   }
  99. }
  100.